package com.haixiaoke.saas.lock.service.impl;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.constant.DeviceLockConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.lock.domain.Group;
import com.haixiaoke.saas.lock.mapper.GroupMapper;
import com.haixiaoke.saas.lock.service.IGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分组Service业务层处理
 *
 * @author panda
 * @date 2025-04-02
 */
@Service
public class GroupServiceImpl implements IGroupService {
    @Autowired
    private GroupMapper groupMapper;

    /**
     * 查询分组
     *
     * @param groupId 分组主键
     * @return 分组
     */
    @Override
    public Group selectGroupByGroupId(Long groupId) {
        return groupMapper.selectGroupByGroupId(groupId);
    }

    /**
     * 查询分组列表
     *
     * @param group 分组
     * @return 分组
     */
    @Override
    public List<Group> selectGroupList(Group group) {
        return groupMapper.selectGroupList(group);
    }

    /**
     * 新增分组
     *
     * @param group 分组
     * @return 结果
     */
    @Override
    public AjaxResult insertGroup(Group group) {
        if (StrUtil.isNotEmpty(group.getGroupName()) && group.getGroupName().length() > 15) {
            return AjaxResult.error("分组名称超限");
        }
        group.setCreateTime(DateUtils.getNowDate());
        int result = groupMapper.insertGroup(group);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改分组
     *
     * @param group 分组
     * @return 结果
     */
    @Override
    public AjaxResult updateGroup(Group group, Long loginUserId) {
        if (StrUtil.isNotEmpty(group.getGroupName()) && group.getGroupName().length() > 15) {
            return AjaxResult.error("分组名称超限");
        }
        Group byGroupId = groupMapper.selectGroupByGroupId(group.getGroupId());
        if (ObjectUtil.isEmpty(byGroupId)) {
            return AjaxResult.error("分组不存在");
        }
        if (!Objects.equals(byGroupId.getUserId(), loginUserId)) {
            return AjaxResult.error("不能修改别人的分组");
        }
        if (StrUtil.equals(DeviceLockConstants.GROUP_TYPE_NON_DEFAULT, byGroupId.getGroupType())) {
            return AjaxResult.error("默认分组不能编辑");
        }
        group.setUpdateTime(DateUtils.getNowDate());
        int result = groupMapper.updateGroup(group);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除分组
     *
     * @param groupIds 需要删除的分组主键
     * @return 结果
     */
    @Override
    public int deleteGroupByGroupIds(Long[] groupIds) {
        return groupMapper.deleteGroupByGroupIds(groupIds);
    }

    /**
     * 删除分组信息
     *
     * @param groupId 分组主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteGroupByGroupId(Long groupId, Long loginUserId) {
        Group byGroupId = groupMapper.selectGroupByGroupId(groupId);
        if (ObjectUtil.isEmpty(byGroupId)) {
            return AjaxResult.error("分组不存在");
        }
        int count = groupMapper.countByGroupId(groupId);
        if (count > 0) {
            return AjaxResult.error("该分组下存在设备，不能删除");
        }
        int result = groupMapper.deleteGroupByGroupId(groupId);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional
    public AjaxResult moveGroup(Group group) {
        if (CollectionUtil.isEmpty(group.getFaceIdList())) {
            return AjaxResult.error("请选择要移动的锁");
        }
        if (group.getGroupId() == null) {
            return AjaxResult.error("请选择分组");
        }
        Group byGroupId = groupMapper.selectGroupByGroupId(group.getGroupId());
        if (ObjectUtil.isEmpty(byGroupId)) {
            return AjaxResult.error("分组不存在");
        }
        int result = groupMapper.moveGroup(group);
        if (result != group.getFaceIdList().size()) {
            throw new ServiceException("移动失败");
        }
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public Group selectDefaultGroupByUserId(Long userId) {
        return groupMapper.selectDefaultGroupByUserId(userId);
    }

    @Override
    public List<Group> selectGroupByGroupIds(List<Long> userIds) {
        return groupMapper.selectGroupByGroupIds(userIds);
    }
}